package com.heimo.gogocheque.core;

public class EnglishDecimalFormat {
	private static final String[] majorNames = { "", " thousand", " million",
			" billion", " trillion", " quadrillion", " quintillion" };

	private static final String[] tensNames = { "", " ten", " twenty",
			" thirty", " fourty", " fifty", " sixty", " seventy", " eighty",
			" ninety" };

	private static final String[] numNames = { "", " one", " two", " three",
			" four", " five", " six", " seven", " eight", " nine", " ten",
			" eleven", " twelve", " thirteen", " fourteen", " fifteen",
			" sixteen", " seventeen", " eighteen", " nineteen" };

	private String convertLessThanOneThousand(int number) {
		String soFar;

		if (number % 100 < 20) {
			soFar = numNames[number % 100];
			number /= 100;
		} else {
			soFar = numNames[number % 10];
			number /= 10;

			soFar = tensNames[number % 10] + soFar;
			number /= 10;
		}
		if (number == 0)
			return soFar;
		return numNames[number] + " hundred" + soFar;
	}

	public String convert(int number) {
		/* special case */
		if (number == 0) {
			return "zero";
		}

		String prefix = "";

		if (number < 0) {
			number = -number;
			prefix = "negative";
		}

		String soFar = "";
		int place = 0;

		do {
			int n = number % 1000;
			if (n != 0) {
				String s = convertLessThanOneThousand(n);
				soFar = s + majorNames[place] + soFar;
			}
			place++;
			number /= 1000;
		} while (number > 0);
		
		String ans = (prefix + soFar).trim();
		
		if(ans.equalsIgnoreCase("one")){
			return ans+" dollar only";
		}
		return ans+" dollars only";
	}
}
